package com.mixsmart.web.plugins.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mixsmart.commonweb.IWebConstant;
import com.mixsmart.commonweb.plugins.IPlugin;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.helper.ObjectTreeHelper;
import com.mixsmart.core.service.IResQueryService;
import com.mixsmart.utils.StringUtils;
import com.mixsmart.web.plugins.bean.JqGridData;
import com.mixsmart.web.plugins.bean.JqGridRows;

/**
 * JqGrid插件服务类
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2016年2月2日
 */
@Service("jqGridTreePlugin")
public class JqGridTreeService implements IPlugin {

	private static final Logger logger = Logger.getLogger(JqGridTreeService.class);
	
	@Autowired
	private IResQueryService resQueryServ;
	@Autowired
	private ObjectTreeHelper objTreeHelper;
	
	@SuppressWarnings("unchecked")
	@Override
	public <E> MixResponse<E> getDatas(String resName,HashMap<String, Object> params) {
		MixResponse<JqGridData> smartResp = new MixResponse<JqGridData>();
		if( StringUtils.isNotEmpty(resName)) {
			/*boolean isExpand = false;
			boolean isAsyn = false;
			if(null != params && !params.isEmpty()) {
				String strExpand = StringUtils.handNull(params.remove("expanded"));
				String strAsyn = StringUtils.handNull(params.remove("loaded"));
				if("1".equals(strExpand)) {
					isExpand = true;
				}
				if("1".equals(strAsyn)) {
					isAsyn = true;
				}
			}*/
			MixResponse<Object> mixResp = resQueryServ.getDatas(resName, params);
			if(IWebConstant.OP_SUCCESS.equals(mixResp.getResult())) {
				JqGridData jqGridData = new JqGridData();
				jqGridData.setRecords(mixResp.getTotalNum());
				jqGridData.setTotal(mixResp.getTotalPage());
				List<Object> objs = mixResp.getDatas();
				if(null != objs && objs.size()>0) {
					List<JqGridRows> jqGridRows = new ArrayList<JqGridRows>();
					JqGridRows jqGridRow = null;
					try {
						objs = objTreeHelper.outPutTree(objs);
						for (Object obj : objs) {
							Object[] objArray = (Object[])obj;
							jqGridRow = new JqGridRows();
							jqGridRow.setId(StringUtils.handNull(objArray[0]));
							jqGridRow.setCell(Arrays.copyOfRange(objArray, 2, objArray.length));
							jqGridRows.add(jqGridRow);
						}
						jqGridData.setRows(jqGridRows);
						jqGridRow = null;
						smartResp.setData(jqGridData);
						smartResp.setResult(IWebConstant.OP_SUCCESS);
						smartResp.setMsg(IWebConstant.OP_SUCCESS_MSG);
						jqGridRows = null;
						jqGridData = null;
					} catch (Exception e) {
						e.printStackTrace();
					}
				}//if
				objs = null;
			}
		}
		return (MixResponse<E>) smartResp;
	}

	@Override
	public <E> MixResponse<E> getDatasPage(String resName, HashMap<String, Object> params, int page, int rows) {
		logger.error("树形结构不支持分页...");
		return null;
	}
	
}
